package com.alogic.xscript.plugins;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.*;
import org.apache.commons.lang3.StringUtils;

/**
 * 第二种Appender实现
 *
 * @version  1.6.13.26 [20201114 duanyy] <br>
 * - 增加动态参数的appender <br>
 */
public class UrlAppender2 extends Segment {
    /**
     * 输出变量id
     */
    protected String $id;

    /**
     * URL的前缀
     */
    protected String $base = "";

    protected String encoding = "utf-8";

    /**
     * 是否append到hash端
     */
    protected String $toHash = "false";

    protected String cid = "$url-appender";

    public UrlAppender2(String tag, Logiclet p) {
        super(tag, p);

        registerModule("param",Param.class);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $id = PropertiesConstants.getString(p,"id","$" + this.getXmlTag(),true);
        $base = PropertiesConstants.getRaw(p,"base",$base);
        encoding = PropertiesConstants.getString(p, "encoding", encoding);
        cid = PropertiesConstants.getString(p,"cid",cid,true);
        $toHash = PropertiesConstants.getRaw(p,"toHash",$toHash);
    }

    @Override
    protected void onExecute(final XsObject root, final XsObject current, final LogicletContext ctx,
                             final ExecuteWatcher watcher) {
        String id = PropertiesConstants.transform(ctx,$id,"");
        if (StringUtils.isNotEmpty(id)){
            UrlBuilder builder = new UrlBuilder(PropertiesConstants.transform(ctx,$base,""));

            try {
                ctx.setObject(cid,builder);

                super.onExecute(root,current,ctx,watcher);

                ctx.SetValue(id,
                        builder.build(encoding,PropertiesConstants.transform(ctx,$toHash,false))
                );
            }finally{
                ctx.removeObject(cid);
            }
        }
    }

    /**
     * 参数
     */
    public static class Param extends AbstractLogiclet {
        protected String $id;
        protected String $value;

        protected String pid = "$url-appender";

        public Param(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p) {
            super.configure(p);
            $id = PropertiesConstants.getRaw(p, "id", "");
            $value = PropertiesConstants.getRaw(p, "value", "");
            pid = PropertiesConstants.getString(p, "pid", pid, true);
        }

        @Override
        protected void onExecute(final XsObject root, final XsObject current, final LogicletContext ctx,
                                 final ExecuteWatcher watcher) {
            UrlBuilder builder = ctx.getObject(pid);
            if (builder != null) {
                String id = PropertiesConstants.transform(ctx, $id, "");
                String value = PropertiesConstants.transform(ctx, $value, "");
                if (StringUtils.isNotEmpty(id)) {
                    builder.addParameters(id, value);
                }
            }
        }
    }
}
